#include <gtest/gtest.h>
#include <string>
#include "xdb_config.h"
#include <fstream>

namespace {
using namespace std;
using namespace xdragonball;


// The fixture for testing class Config.
class ConfigTest : public ::testing::Test {
protected:
	// You can remove any or all of the following functions if its body
	// is empty.

	ConfigTest() {
		// You can do set-up work for each test here.
	}

	virtual ~ConfigTest() {
		// You can do clean-up work that doesn't throw exceptions here.
	}

	// If the constructor and destructor are not enough for setting up
	// and cleaning up each test, you can define the following methods:

	virtual void SetUp() {
		// Code here will be called immediately after the constructor (right
		// before each test).
	}

	virtual void TearDown() {
		// Code here will be called immediately after each test (right
		// before the destructor).
	}

	// Objects declared here can be used by all tests in the test case for Config.
};

// Tests that the Config::Bar() method does Abc.
TEST_F(ConfigTest, to_int32) {
	Config cfg = 1;
	cfg = 2;
	EXPECT_EQ(cfg.to_int32(), 2);
}

TEST_F(ConfigTest, ConfigLoader)
{
	std::string conf = "test_config.conf.1";
	std::ofstream out(conf.c_str());
	out<<"#hello"<<std::endl;
	out<<"A : 1"<<std::endl;
	out<<"server : 10"<<std::endl;
	out<<"server.1.ip : 127.0.0.1"<<std::endl;
	out<<"server.2.ip : 127.0.0.2"<<std::endl;
	out<<"server.1.port : 1234"<<std::endl;
	out<<"server.2.port : 12345"<<std::endl;
	out.close();
	Config cfg;
	EXPECT_EQ(ConfigLoader(cfg, conf), 0);
	EXPECT_EQ(cfg["A"].to_int32(), 1);
	EXPECT_EQ(cfg["server"]["1"]["ip"].to_string(), "127.0.0.1");
	EXPECT_EQ(cfg["server"]["2"]["ip"].to_string(), "127.0.0.2");
	EXPECT_EQ(cfg["server"]["1"]["port"].to_int32(), 1234);
	EXPECT_EQ(cfg["server"]["2"]["port"].to_int32(), 12345);

	//EXPECT_EQ(cfg["server"].to_string(), "server");
	//EXPECT_EQ(cfg["server"]["1"].to_int32(), 1);
	//EXPECT_EQ(cfg["server"]["2"].to_int32(), 2);
}

}  // namespace

int main(int argc, char **argv) {
	::testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}
